<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">


            <el-tree
                    :data="treedata"

                    node-key="id"
                    default-expand-all
                    :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
                  type="text"
                  size="mini"
                  @click="toAppend(node,data)">
             <i class="el-icon-circle-plus-outline"></i>
          </el-button>
          <el-button
                  type="text"
                  size="mini"
                  @click="remove(node, data)">
            <i class="el-icon-delete"></i>
          </el-button>
              <el-button
                      type="text"
                      size="mini"
                      @click="handleUpdate(node, data)">
            <i class="el-icon-edit"></i>
          </el-button>
        </span>
      </span>
            </el-tree>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <!--Dialog 对话框-->
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <!--Form 表单-->
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="菜单修改" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/status.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            treedata:[],
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '菜单名为必填项', trigger: 'blur'}],
                linkUrl: [{required: true, message: '访问路径为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
           this.updatePage()

        },
        methods: {
            toAppend(node,data){
                    this.resetForm();
                    this.formData.parentMenuId=data.id;
                    this.dialogFormVisible = true;
            },
            resetForm() {
                this.formData = {};
            },
            updatePage() {
                axios.get('/menu/getAllMenu.do').then((res)=>{
                    this.treedata=res.data.data;
                })
            },
            // 弹出编辑窗口
            handleUpdate(node,data) {
                axios.get("/menu/findById.do", {
                    params: {
                        id: data.id
                    }
                }).then((res) => {
                    this.formData = res.data.data;
                    this.dialogFormVisible4Edit = true;
                });

            },
            handleEdit() {
                axios.put("/menu/edit.do", this.formData)
                    .then((res) => {
                        if (res.data.flag) {
                            //关闭弹框
                            this.dialogFormVisible4Edit = false;
                            // 刷新页面
                            this.updatePage();
                        }

                        //提示信息
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success' : 'error'
                        });
                    });
            },
            handleAdd() {
                this.$refs.dataAddForm.validate((flag) => {
                    if (flag) {
                        axios.post('/menu/add.do', this.formData)
                            .then((response) => {
                                console.log(response);
                                if (response.data.flag) {
                                    //关闭弹窗
                                    this.dialogFormVisible = false;
                                    //刷新页面
                                    this.updatePage();
                                }
                                //提示信息
                                this.$message({
                                    message: response.data.message,
                                    type: response.data.flag ? 'success' : 'error'
                                });
                            })
                            .catch((error) => {
                                console.log(error);
                            });
                    } else {
                        this.$message.error('表单检验失败');
                        return flase;//固定写法检验失败返回false
                    }
                })
            },

            remove(node,data){
                this.$confirm('是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete("/menu/delete.do", {
                        params: {
                            id: data.id
                        }
                    })
                        .then((res) => {
                            //刷新页面
                            this.updatePage();
                            //提示用户
                            this.$message({
                                type: res.data.flag ? 'success' : 'error',
                                message: res.data.message
                            });
                        })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
            //编辑
            // handleEdit() {
            //     axios.put("/permission/edit.do", this.formData)
            //         .then((res) => {
            //             if (res.data.flag) {
            //                 //关闭弹框
            //                 this.dialogFormVisible4Edit = false;
            //                 // 刷新页面
            //                 this.findPage();
            //             }
            //
            //             //提示信息
            //             this.$message({
            //                 message: res.data.message,
            //                 type: res.data.flag ? 'success' : 'error'
            //             });
            //         });
            // },
            // //添加

            // //分页查询

            // // 重置表单

            // // 弹出添加窗口
            // handleCreate() {
            //     this.resetForm();
            //     this.dialogFormVisible = true;
            // },
            // // 弹出编辑窗口
            // handleUpdate(row) {
            //     console.log(row.id);
            //     axios.get("/permission/findById.do", {
            //         params: {
            //             id: row.id
            //         }
            //     }).then((res) => {
            //         this.formData = res.data.data;
            //         this.dialogFormVisible4Edit = true;
            //     });
            //
            // },
            // //切换页码
            // handleCurrentChange(currentPage) {
            //     // console.log(currentPage);
            //     this.pagination.currentPage = currentPage;
            //     this.findPage();
            // },
            // // 删除
            // handleDelete(row) {
            //     console.log(row);
            //     this.$confirm('是否删除?', '提示', {
            //         confirmButtonText: '确定',
            //         cancelButtonText: '取消',
            //         type: 'warning'
            //     }).then(() => {
            //         axios.delete("/permission/delete.do", {
            //             params: {
            //                 id: row.id
            //             }
            //         })
            //             .then((res) => {
            //                 console.log(res);
            //                 //刷新页面
            //                 this.findPage();
            //                 //提示用户
            //                 this.$message({
            //                     type: res.data.flag ? 'success' : 'error',
            //                     message: res.data.message
            //                 });
            //             })
            //     }).catch(() => {
            //         this.$message({
            //             type: 'info',
            //             message: '已取消删除'
            //         });
            //     });
            // }
        }
    })
</script>
</html>